package com.android.launcher3;

import android.content.Context;
import android.content.Intent;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;

/**
 * 完整 Represents a set of icons chosen by the user or generated by the system.
 */
public class Folder extends LinearLayout implements DragSource,
		OnItemLongClickListener, OnItemClickListener, OnClickListener,
		View.OnLongClickListener {

	protected AbsListView mContent;
	protected Button mCloseButton;
	protected DragController mDragger;
	protected FolderInfo mInfo;
	protected Launcher mLauncher;

	/** Which item is being dragged */
	protected ShortcutInfo mDragItem;

	/**
	 * Used to inflate the Workspace from XML.
	 * 
	 * @param context
	 *            The application's context.
	 * @param attrs
	 *            The attribtues set containing the Workspace's customization
	 *            values.
	 */
	public Folder(final Context context, final AttributeSet attrs) {
		super(context, attrs);
		setAlwaysDrawnWithCacheEnabled(false);
	}

	public void onClick(final View v) {
		mLauncher.closeFolder(this);
	}

	public void onDropCompleted(final View target, final boolean success,
			final Object dragInfo) {
	}

	public void onItemClick(final AdapterView<?> paramAdapterView,
			final View paramView, final int paramInt, final long paramLong) {
		ShortcutInfo localShortcutInfo = (ShortcutInfo) paramAdapterView
				.getItemAtPosition(paramInt);
		int[] arrayOfInt = new int[2];
		paramView.getLocationOnScreen(arrayOfInt);

		Intent localIntent1 = localShortcutInfo.intent;
		int i = arrayOfInt[0];
		int j = arrayOfInt[1];
		int k = arrayOfInt[0];
		int m = paramView.getWidth();
		int n = k + m;
		int i1 = arrayOfInt[1];
		int i2 = paramView.getHeight();
		int i3 = i1 + i2;
		Rect localRect = new Rect(i, j, n, i3);
		localIntent1.setSourceBounds(localRect);

		Launcher localLauncher = this.mLauncher;
		Intent localIntent2 = localShortcutInfo.intent;
		localLauncher.startActivitySafely(localIntent2, localShortcutInfo);
	}

	public boolean onItemLongClick(final AdapterView<?> paramAdapterView,
			final View paramView, final int paramInt, final long paramLong) {
		if (!paramView.isInTouchMode()) {
			return false;
		}

		ShortcutInfo localShortcutInfo = (ShortcutInfo) paramAdapterView
				.getItemAtPosition(paramInt);
		this.mDragger.startDrag(paramView, this, localShortcutInfo,
				DragController.DRAG_ACTION_COPY);
		this.mLauncher.closeFolder(this);
		this.mDragItem = localShortcutInfo;

		return true;
	}

	public boolean onLongClick(final View v) {
		mLauncher.closeFolder(this);
		mLauncher.showRenameDialog(mInfo);

		return true;
	}

	public void setDragger(final DragController dragger) {
		mDragger = dragger;
	}

	@Override
	protected void onFinishInflate() {
		super.onFinishInflate();

		mContent = (AbsListView) findViewById(R.id.folder_content);
		mContent.setOnItemClickListener(this);
		mContent.setOnItemLongClickListener(this);

		mCloseButton = (Button) findViewById(R.id.folder_close);
		mCloseButton.setOnClickListener(this);
		mCloseButton.setOnLongClickListener(this);
	}

	/**
	 * 完整
	 * 
	 * @param info
	 */
	void bind(final FolderInfo info) {
		mInfo = info;
		mCloseButton.setText(info.title);
	}

	/**
	 * DOCUMENT ME!
	 * 
	 * @return the FolderInfo object associated with this folder
	 */
	FolderInfo getInfo() {
		return mInfo;
	}

	void notifyDataSetChanged() {
		((BaseAdapter) mContent.getAdapter()).notifyDataSetChanged();
	}

	void onClose() {
		final Workspace workspace = mLauncher.getWorkspace();
		workspace.getChildAt(workspace.getCurrentScreen()).requestFocus();
	}

	// When the folder opens, we need to refresh the GridView's selection by
	void onOpen() {
		mContent.requestLayout();
	}

	/**
	 * Sets the adapter used to populate the content area. The adapter must only
	 * contains ApplicationInfo items.
	 * 
	 * @param adapter
	 *            The list of applications to display in the folder.
	 */
	void setContentAdapter(final BaseAdapter adapter) {
		mContent.setAdapter(adapter);
	}

	void setLauncher(final Launcher launcher) {
		mLauncher = launcher;
	}
}
